﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ft_module_parser.aftconversion.ppd
{
    static class ytparse
    {
        private const string YoutubeLinkRegex = "(?:.+?)?(?:\\/v\\/|watch\\/|\\?v=|\\&v=|youtu\\.be\\/|\\/v=|^youtu\\.be\\/)([a-zA-Z0-9_-]{11})+";
        private static Regex regexExtractId = new Regex(YoutubeLinkRegex, RegexOptions.Compiled);
        private static string[] validAuthorities = { "youtube.com", "www.youtube.com", "youtu.be", "www.youtu.be" };

        public static Bitmap getThumb(string yturi)
        {
            string vdid;
            try
            {
                vdid = ExtractVideoIdFromUri(new Uri(yturi));
            } catch
            {
                return null;
            }
            if (vdid == null)
                return null;
            if (ytparse.CheckUri("https://img.youtube.com/vi/" + vdid + "/maxresdefault.jpg"))
            {
                System.Net.WebRequest request =
                System.Net.WebRequest.Create(
                "https://img.youtube.com/vi/" + vdid + "/maxresdefault.jpg");
                System.Net.WebResponse response = request.GetResponse();
                System.IO.Stream responseStream =
                    response.GetResponseStream();
                Bitmap bm1 = new Bitmap(responseStream);
                return bm1;
            }
            else
            {
                System.Net.WebRequest request =
                System.Net.WebRequest.Create(
                "https://img.youtube.com/vi/" + vdid + "/0.jpg");
                System.Net.WebResponse response = request.GetResponse();
                System.IO.Stream responseStream =
                    response.GetResponseStream();
                Bitmap bm1 = new Bitmap(responseStream);
                return bm1;
            }
        }

        public static bool CheckUri(string uri)
        {
            bool ret = false;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "GET";
            try
            {
                request.GetResponse();
                ret = true;
            }
            catch
            {
                ret = false;
            }
            return ret;
        }
        public static string ExtractVideoIdFromUri(Uri uri)
        {
            try
            {
                string authority = new UriBuilder(uri).Uri.Authority.ToLower();

                //check if the url is a youtube url
                if (validAuthorities.Contains(authority))
                {
                    //and extract the id
                    var regRes = regexExtractId.Match(uri.ToString());
                    if (regRes.Success)
                    {
                        return regRes.Groups[1].Value;
                    }
                }
            }
            catch { }


            return null;
        }
    }
}
